﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XQ.WeChatOfficialAccount.DAL.DAL.Common;
using XQ.WeChatOfficialAccount.DAL.Model;
using XQ.WeChatOfficialAccount.DAL.Model.Business;
using XQ.WeChatOfficialAccount.DAL.Models;

namespace XQ.WeChatOfficialAccount.DAL.DAL.Business
{
    /// <summary>
    /// 文章数据操作类
    /// </summary>
    public class ContextsDal:BaseDal<Contexts>
    {
        /// <summary>
        /// 获取文章列表
        /// </summary>
        /// <param name="ssm"></param>
        /// <param name="page"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        public List<ContextsModel> GetList(ContextsSearchModel ssm, Pagination page, TokenModel token)
        {
            using (WeChatOfficialAccountEntities woae = new WeChatOfficialAccountEntities())
            {
                //分页信息
                StringBuilder sqlPage = new StringBuilder();
                sqlPage.Append(" select count(1) ");
                sqlPage.Append(" from Contexts c");
                sqlPage.Append(" where 1=1 ");
                //类别ID
                if (ssm.CategoryTypeID > 0)
                {
                    sqlPage.Append(" and c.CategoryTypeID=" + ssm.CategoryTypeID);
                }
                //文章标题
                if (!string.IsNullOrEmpty(ssm.Title))
                {
                    sqlPage.Append(" and c.Title like '%" + ssm.Title + "%'");
                }
                //文章内容
                if (!string.IsNullOrEmpty(ssm.Context))
                {
                    sqlPage.Append(" and c.Context like '%" + ssm.Context + "%'");
                }
                
                //发布日期起
                if (ssm.SendTimeStart.Year != 1970)
                {
                    sqlPage.Append(" and c.sendTime >= '" + ssm.SendTimeStart + "'");
                }
                //发布日期止
                if (ssm.SendTimeEnd.Year != 9999)
                {
                    sqlPage.Append(" and c.sendTime <= '" + ssm.SendTimeEnd.ToString("yyyy-MM-dd") + " 23:59:59'");
                }

                //是否发布
                if (ssm.IsSend == true)
                {
                    sqlPage.Append(" and c.isSend=1 ");
                }
                else if (ssm.IsSend == false)
                {
                    sqlPage.Append(" and (c.isSend=0 or c.isSend is null) ");
                }
                                
                int allCount = woae.Database.SqlQuery<int>(sqlPage.ToString()).First();
                page.RecordCount = allCount;
                page.PageCount = (int)Math.Ceiling((decimal)page.RecordCount / (decimal)ssm.pageSize);
                page.PageIndex = ssm.index;
                page.PageSize = ssm.pageSize;

                //数据列表
                StringBuilder sql = new StringBuilder();
                sql.Append("select top " + ssm.pageSize + " * from ");
                sql.Append(" (select row_number() over(order by c.id desc) as rowNum, ");
                sql.Append(" c.*,");
                sql.Append(" (select CategoryName from Category where id=c.CategoryTypeID) as CategoryTypeName");
                sql.Append(" from Contexts c ");
                sql.Append(" where 1=1 ");

                //类别ID
                if (ssm.CategoryTypeID > 0)
                {
                    sql.Append(" and c.CategoryTypeID=" + ssm.CategoryTypeID);
                }
                //文章标题
                if (!string.IsNullOrEmpty(ssm.Title))
                {
                    sql.Append(" and c.Title like '%" + ssm.Title + "%'");
                }
                //文章内容
                if (!string.IsNullOrEmpty(ssm.Context))
                {
                    sql.Append(" and c.Context like '%" + ssm.Context + "%'");
                }

                //发布日期起
                if (ssm.SendTimeStart.Year != 1970)
                {
                    sql.Append(" and c.sendTime >= '" + ssm.SendTimeStart + "'");
                }
                //发布日期止
                if (ssm.SendTimeEnd.Year != 9999)
                {
                    sql.Append(" and c.sendTime <= '" + ssm.SendTimeEnd.ToString("yyyy-MM-dd") + " 23:59:59'");
                }

                //是否发布
                if (ssm.IsSend == true)
                {
                    sql.Append(" and c.isSend=1 ");
                }
                else if (ssm.IsSend == false)
                {
                    sql.Append(" and (c.isSend=0 or c.isSend is null) ");
                }
                sql.Append(") t1 ");
                sql.Append("where rowNum>" + (ssm.index - 1) * ssm.pageSize);
                LogBll.Instance.WriteLog(LogBll.LogType.Search, "", token.UID, "获取文章列表SQL", "", "", "", sql.ToString());
                return woae.Database.SqlQuery<ContextsModel>(sql.ToString()).ToList();
            }
        }

        /// <summary>
        /// 更新类别
        /// </summary>
        /// <param name="saveModel"></param>
        /// <returns></returns>
        public int UpdateContexts(ContextsSaveModel saveModel)
        {
            using (WeChatOfficialAccountEntities wae = new WeChatOfficialAccountEntities())
            {
                Contexts curContexts = wae.Contexts.FirstOrDefault(c => c.id == saveModel.id);
                curContexts.CategoryTypeID = saveModel.CategoryTypeID;
                curContexts.Context = saveModel.Context;
                curContexts.isSend = saveModel.isSend;
                curContexts.SendTime = saveModel.SendTime;
                curContexts.Title = saveModel.Title;
                curContexts.TitleImage = saveModel.TitleImage;
                curContexts.ViewCount = saveModel.ViewCount;
                curContexts.UpdateTime = saveModel.UpdateTime;
                curContexts.UpdateUserID = saveModel.UpdateUserID;

                return wae.SaveChanges();
            }
        }
    }
}
